// 地图服务器地址
export const mapServer = "http://192.168.111.81:19527";
// 地图默认中心点
export const defaultCoordinates = [126.1722017295496, 43.7084630627787];
// 地球默认范围为中国大陆
export const defaultBounds = [
  [73.4977, 18.163],
  [135.095, 53.56]
];
// 本机地址
export const locationIp = window.location.origin;
// 地图默认缩放级别
export const defaultZoom = 1;
//  本地mapbox 字体地址
export const glyphsUrl = `${locationIp}/mapbox/offlinemap/fonts/{fontstack}/{range}.pbf`;
//   本地mapbox 雪碧图地址
export const spriteUrl = `${locationIp}/mapbox/offlinemap/sprites/sprite`;

window.map = null; // 地图实例

// 线上瓦片
// 天地图矢量地图URL
export const TianDiTuVectorURL =
  "http://t0.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=b5c6d22f3ea7a78d2526bcc2552882ef";
// 天地图影像地图URL
export const TianDiTuSatelliteURL =
  "http://t0.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=b5c6d22f3ea7a78d2526bcc2552882ef";
// 天地图地形图URL
export const TianDiTuTerrainURL =
  "http://t0.tianditu.com/DataServer?T=ter_w&x={x}&y={y}&l={z}&tk=53e687e1a5430675668e3c4e9cf80487";
// 高德卫星影像URL
export const GaodeSatelliteURL = "https://webst01.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}";
// OSM Transport图URL
export const OSMTransportURL =
  "https://c.tile.thunderforest.com/transport/{z}/{x}/{y}.png?apikey=a5dd6a2f1c934394bce6b0fb077203eb";
//OSM Transport Dark图URL
export const OSMTransportDarkURL =
  "https://c.tile.thunderforest.com/transport-dark/{z}/{x}/{y}.png?apikey=a5dd6a2f1c934394bce6b0fb077203eb";
// OSM Landscape图URL
export const OSMLandscapeURL =
  "https://b.tile.thunderforest.com/landscape/{z}/{x}/{y}.png?apikey=a5dd6a2f1c934394bce6b0fb077203eb";
// OSM大字体地形图URL
export const OSMOutdoorsURL = "https://tile.tracestrack.com/topo__/{z}/{x}/{y}.png?key=383118983d4a867dd2d367451720d724";
// OSM人道救援图URL
export const OSMHOTURL = "https://tile-a.openstreetmap.fr/hot/{z}/{x}/{y}.png";
// 高德大字体电子地图URL
export const GaodeElectronURL = "http://wprd04.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}";
// 高德路网URL
export const GaodeRoadURL = "https://wprd01.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=2&style=8<ype=11";
// 高德地名路网URL
export const GaodePlaceURL = "https://wprd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}";
// 腾讯电子地图
export const TencentElectronURL = "https://rt1.map.gtimg.com/tile?z={z}&x={x}&y={-y}&styleid=0&version=256";
// 冷色地图URL
export const ColdColorURL =
  "https://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/WMTS/tile/1.0.0/Canvas_World_Light_Gray_Base/default/default028mm/{z}/{y}/{x}/";
// 暗黑地图URL
export const DarkMapURL =
  "https://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Dark_Gray_Base/MapServer/WMTS/tile/1.0.0/Canvas_World_Dark_Gray_Base/default/default028mm/{z}/{y}/{x}/";
// 海洋渲染图URL
export const OceanRenderURL =
  "https://server.arcgisonline.com/ArcGIS/rest/services/Ocean/World_Ocean_Base/MapServer/tile/{z}/{y}/{x}";
// 360点击地图URL
export const QihooMapURL = "http://map4.qhimg.com/sotile/ver12/2/{z}/?x={x}&y={-y}";
// ArcGIS World卫星图URL
export const ArcGISWorldSatelliteURL =
  "https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}";
// 10m等高线地图URL
export const ContourMapURL = "https://c.tile.opentopomap.org/{z}/{x}/{y}.png";
// 土地利用分类地图URL
export const LandUseMapURL =
  "https://services.terrascope.be/wmts/v2?layer=WORLDCOVER_2021_MAP&style=&tilematrixset=EPSG:3857&Service=WMTS&Request=GetTile&Version=1.0.0&Format=image/png&TileMatrix=EPSG:3857:{z}&TileCol={x}&TileRow={y}&TIME=2022-10-31";
// OpenStreetMap标准地图URL
export const OpenStreetMapURL = "https://tile.openstreetmap.bzh/br/{z}/{x}/{y}.png";
// OSM冬日地图URL
export const OSMWinterURL = "https://w3.outdooractive.com/map/v1/png/osm_winter/{z}/{x}/{y}/t.png?project=api-dev-oa";
// OMS户外地图URL
export const OSMOutdoorsURL2 = "http://a.tile.thunderforest.com/outdoors/{z}/{x}/{y}.png?apikey=6170aad10dfd42a38d4d8c709a536f38";

// 地形
// 地形渲染图1URL
export const TerrainRenderURL1 =
  "https://services.arcgisonline.com/ArcGIS/rest/services/NatGeo_World_Map/MapServer/tile/{z}/{y}/{x}";
// 地形渲染图2URL
export const TerrainRenderURL2 =
  "https://services.arcgisonline.com/ArcGIS/rest/services/USA_Topo_Maps/MapServer/tile/{z}/{y}/{x}";
// 地形渲染图3URL
export const TerrainRenderURL3 =
  "https://server.arcgisonline.com/ArcGIS/rest/services/World_Shaded_Relief/MapServer/tile/{z}/{y}/{x}";
// OpenCycle地形图URL
export const OpenCycleTerrainURL =
  "http://a.tile.thunderforest.com/cycle/{z}/{x}/{y}.png?apikey=a5dd6a2f1c934394bce6b0fb077203eb";
// OSM地形渲染图URL
export const OSMTerrainRenderURL = "http://s0.outdooractive.com/osm/OSMSummer/{z}/{x}/{y}.png";

// 地图集合 name: 地图名称，url: 地图URL，key: 地图属性名
export const mapConfigList = [
  {
    name: "天地图矢量地图",
    url: TianDiTuVectorURL,
    key: "tianDiTuVectorMap",
    thumbnail: "/map-thumbnails/tian-di-tu-vector.jpg"
  },
  {
    name: "天地图影像地图",
    url: TianDiTuSatelliteURL,
    key: "TtanDiTuSatelliteMap",
    thumbnail: "/map-thumbnails/tian-di-tu-vector.jpg"
  },
  {
    name: "高德卫星影像",
    url: GaodeSatelliteURL,
    key: "gaodeSatelliteMap",
    thumbnail: "/map-thumbnails/gaode-satellite.jpg"
  },
  {
    name: "OSM Transport图",
    url: OSMTransportURL,
    key: "osmTransportMap",
    thumbnail: "/map-thumbnails/osm-transport.jpg"
  },
  {
    name: "OSM Transport Dark图",
    url: OSMTransportDarkURL,
    key: "osmTransportDarkMap",
    thumbnail: "/map-thumbnails/osm-transport-dark.jpg"
  },
  {
    name: "OSM Landscape图",
    url: OSMLandscapeURL,
    key: "osmLandscapeMap",
    thumbnail: "/map-thumbnails/osm-landscape.jpg"
  },
  // {
  //   name: "OSM人道救援图",
  //   url: OSMHOTURL,
  //   key: "osmHotMap",
  //   thumbnail: "/map-thumbnails/osm-hot.jpg"
  // },
  {
    name: "高德大字体电子地图",
    url: GaodeElectronURL,
    key: "gaodeElectronMap",
    thumbnail: "/map-thumbnails/gaode-electron.jpg"
  },
  {
    name: "高德路网",
    url: GaodeRoadURL,
    key: "gaodeRoadMap",
    thumbnail: "/map-thumbnails/gaode-road.jpg"
  },
  {
    name: "高德地名路网",
    url: GaodePlaceURL,
    key: "gaodePlaceMap",
    thumbnail: "/map-thumbnails/gaode-place.jpg"
  },
  // 腾讯电子地图
  {
    name: "腾讯电子地图",
    url: TencentElectronURL,
    key: "tencentElectronMap",
    thumbnail: "/map-thumbnails/tencent-electron.jpg"
  },
  {
    name: "OSM大字体地形图",
    url: OSMOutdoorsURL,
    key: "osmOutdoorsMap"
  },
  {
    name: "冷色地图",
    url: ColdColorURL,
    key: "coldMap",
    thumbnail: "/map-thumbnails/cold.jpg"
  },
  {
    name: "暗黑地图",
    url: DarkMapURL,
    key: "darkMap",
    thumbnail: "/map-thumbnails/dark.jpg"
  },
  {
    name: "海洋渲染图",
    url: OceanRenderURL,
    key: "oceanRenderMap",
    thumbnail: "/map-thumbnails/ocean.jpg"
  },
  {
    name: "360点击地图",
    url: QihooMapURL,
    key: "qihooMap",
    thumbnail: "/map-thumbnails/360.jpg"
  },
  {
    name: "ArcGIS世界卫星图",
    url: ArcGISWorldSatelliteURL,
    key: "arcGISWorldSatelliteMap",
    thumbnail: "/map-thumbnails/arcgis-satellite.jpg"
  },
  {
    name: "10m等高线图",
    url: ContourMapURL,
    key: "contourMap",
    thumbnail: "/map-thumbnails/contour.jpg"
  },
  {
    name: "土地利用分类图",
    url: LandUseMapURL,
    key: "landUseMap",
    thumbnail: "/map-thumbnails/landuse.jpg"
  },
  {
    name: "OpenStreetMap标准图",
    url: OpenStreetMapURL,
    key: "openStreetMap",
    thumbnail: "/map-thumbnails/osm-standard.jpg"
  },
  {
    name: "OSM冬日图",
    url: OSMWinterURL,
    key: "osmWinterMap",
    thumbnail: "/map-thumbnails/osm-winter.jpg"
  },
  {
    name: "OSM户外图",
    url: OSMOutdoorsURL2,
    key: "osmOutdoorsMap",
    thumbnail: "/map-thumbnails/osm-outdoor.jpg"
  },
  {
    name: "地形渲染图1",
    url: TerrainRenderURL1,
    key: "terrainRenderMap1",
    thumbnail: "/map-thumbnails/terrain1.jpg"
  },
  {
    name: "地形渲染图2",
    url: TerrainRenderURL2,
    key: "terrainRenderMap2",
    thumbnail: "/map-thumbnails/terrain2.jpg"
  },
  {
    name: "地形渲染图3",
    url: TerrainRenderURL3,
    key: "terrainRenderMap3",
    thumbnail: "/map-thumbnails/terrain3.jpg"
  },
  {
    name: "OpenCycle地形图",
    url: OpenCycleTerrainURL,
    key: "openCycleTerrainMap",
    thumbnail: "/map-thumbnails/opencycle.jpg"
  },
  {
    name: "OSM地形渲染图",
    url: OSMTerrainRenderURL,
    key: "osmTerrainRenderMap",
    thumbnail: "/map-thumbnails/osm-terrain.jpg"
  }
];

// 添加生成地图预览图
export const generateMapThumbnail = async (mapConfig: any) => {
  const { url } = mapConfig;
  const previewZoom = 5;
  // const width = 200;
  // const height = 150;

  // 构建预览URL
  const [lng, lat] = defaultCoordinates;
  const previewUrl = url
    .replace("{z}", previewZoom.toString())
    .replace("{x}", Math.floor(((lng + 180) / 360) * Math.pow(2, previewZoom)).toString())
    .replace(
      "{y}",
      Math.floor(
        ((1 - Math.log(Math.tan((lat * Math.PI) / 180) + 1 / Math.cos((lat * Math.PI) / 180)) / Math.PI) / 2) *
        Math.pow(2, previewZoom)
      ).toString()
    )
    .replace(
      "{-y}",
      Math.floor(
        Math.pow(2, previewZoom) -
        ((1 - Math.log(Math.tan((lat * Math.PI) / 180) + 1 / Math.cos((lat * Math.PI) / 180)) / Math.PI) / 2) *
        Math.pow(2, previewZoom)
      ).toString()
    );

  return previewUrl;
};
